package com.GZHU.controller.save_info;

import com.GZHU.fisco.BlockChainClient;
import com.GZHU.entity.model_record;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.GZHU.fisco.Constant;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Controller
public class Save_certify_info {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @GetMapping("/save_certify_info")
    public void save_model_info(@RequestParam("certify_id") String certify_id,
                                @RequestParam("model_id") String model_id,
                                @RequestParam("certifer_id") String certifer_id,
                                @RequestParam("is_safe") String is_safe,
                                @RequestParam("note") String note,
                                HttpServletResponse response) throws IOException {
        String str=String.format("VALUES ('%s', '%s', '%s','%s',  '%s')", certify_id,model_id,certifer_id,is_safe,note);
        String sql="insert into `certify_info` (`certify_id`, `model_id`, `certifer_id`, `is_safe`, `note`) " +str;
        try {
            jdbcTemplate.execute(sql);
            log.info("data save success!");
            String sql2="SELECT upload_info.model_id,producer_id,verifyer_id,certifer_id FROM upload_info inner JOIN certify_info ON upload_info.model_id=certify_info.model_id\n" +
                    "inner JOIN verify_info ON upload_info.model_id=verify_info.model_id ";
            str= String.format("WHERE upload_info.model_id='%s';",model_id);
            sql2=sql2+str;
            try {
                List static_data=jdbcTemplate.queryForList(sql2);
                if(static_data.size()==0){
                    log.info("static_data not complete");
                }
                else {
                    model_record record=new model_record();
                    Map a=(Map) static_data.get(0);
                    record.setModel_id((String) a.get("model_id"));
                    record.setVerifyer_id((String) a.get("verifyer_id"));
                    record.setProducer_id((String) a.get("producer_id"));
                    record.setCertifer_id((String) a.get("certifer_id"));
                    try {
                        BlockChainClient.record_to_chain(Constant.data_upload,record);
                        log.info("static_data up load to chain success!");
                    }catch (Exception e){
                        log.info("static_data up load to chain fail!");
                    }
                }
            }catch (Exception e){
                log.info("static_data query fail!");
            }
        }catch (Exception e){
            log.info("data save fail!");
        }
        response.sendRedirect("/data_save.html");
    }
}
